//
//  EGORefreshTableHeaderView.m
//  Demo
//
//  Created by Devin Doty on 10/14/09October14.
//  Copyright 2009 enormego. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

#import "LoadMoreTableFooterView.h"

@interface LoadMoreTableFooterView (Private)

- (CGFloat)scrollViewOffsetFromBottom:(UIScrollView *) scrollView;

@end

@implementation LoadMoreTableFooterView

@synthesize delegate=_delegate;
@synthesize state=_state;


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        /* Config Status Updated Label */
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.frame.size.width, LOADMORE_AREA_HEIGTH)];
		label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		label.font = [UIFont systemFontOfSize:13.0f];
		label.shadowOffset = CGSizeMake(0.0f, 1.0f);
		label.backgroundColor = [UIColor clearColor];
		label.textAlignment = NSTextAlignmentCenter;
		[self addSubview:label];
		_statusLabel=label;
        
        /* Config background image */
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, LOADMORE_AREA_HEIGTH)];
        image.backgroundColor = [UIColor clearColor];
        [self addSubview:image];
        _backgroundImage = image;
		
        /* Config activity indicator */
		UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        view.hidesWhenStopped = YES;
        float textWidth = [[NSString stringWithFormat:@"加载中..."] sizeWithFont:[UIFont systemFontOfSize:13.0f]].width;
        view.frame = CGRectMake(APP_CONTENT_WIDTH/2.0f-textWidth/2.0f-28.0f, (LOADMORE_AREA_HEIGTH-20)/2, 20.0f, 20.0f);
		[self addSubview:view];
		_activityView = view;
		
		[self setState:LOADMORENORMAL]; // Also transform the image
        
        /* Configure the default colors and arrow image */
        [self setBackgroundColor:[UIColor clearColor] textColor:[UIColor colorWithWhite:0.71 alpha:1.0] bgImage:nil];
		
    }
	
    return self;
	
}

#pragma mark - Util
- (CGFloat)scrollViewOffsetFromBottom:(UIScrollView *) scrollView
{
    CGFloat scrollAreaContenHeight = scrollView.contentSize.height;
    
    CGFloat visibleTableHeight = MIN(scrollView.bounds.size.height, scrollAreaContenHeight);
    CGFloat scrolledDistance = scrollView.contentOffset.y + visibleTableHeight; // If scrolled all the way down this should add upp to the content heigh.
    
    CGFloat normalizedOffset = scrollAreaContenHeight -scrolledDistance;
    
    return normalizedOffset;
    
}

#pragma mark -
#pragma mark Setters

- (void)setState:(LOADMOREState)aState{
	
	switch (aState) {
		case LOADMORENORMAL:
			_statusLabel.text = @"上拉加载";
			[_activityView stopAnimating];
			break;
		case LOADMOREING:
			
			_statusLabel.text = @"加载中...";
			[_activityView startAnimating];
            
			break;
		case LOADMOREFINISHED:
			
			_statusLabel.text = @"已加载全部";
			[_activityView stopAnimating];
			
			break;
		default:
			break;
	}
	
	_state = aState;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor textColor:(UIColor *) textColor bgImage:(UIImage *) bgImage
{
    self.backgroundColor = backgroundColor? backgroundColor : DEFAULT_BACKGROUND_COLOR;
    _statusLabel.textColor = textColor? textColor: DEFAULT_TEXT_COLOR;
    _statusLabel.shadowColor = [_statusLabel.textColor colorWithAlphaComponent:0.1f];
    _backgroundImage.image = (id)(bgImage ? bgImage.CGImage : nil);
}

#pragma mark -
#pragma mark ScrollView Methods

- (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([self scrollViewOffsetFromBottom:scrollView] <= (0-LOADMORE_AREA_HEIGTH) && !self.hidden && _state == LOADMORENORMAL)
    {
        [self setState:LOADMOREING];
        if ([_delegate respondsToSelector:@selector(loadMoreTableFooterDidTriggerLoadMore:)]) {
            [_delegate loadMoreTableFooterDidTriggerLoadMore:self];
        }
    }
}

#pragma mark -
#pragma mark Dealloc

- (void)dealloc {
	
	_delegate = nil;
	[_activityView release];
	[_statusLabel release];
	[_backgroundImage release];
    [super dealloc];
}


@end
